package info.sean.main;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import info.sean.entity.GsonEntity;
import org.junit.Test;

import java.util.*;

public class GsonMapTest {

    GsonEntity user0 = new GsonEntity(100,"tom0",new Date());
    GsonEntity user1 = new GsonEntity(101,"tom1",new Date());
    GsonEntity user2 = new GsonEntity(102,"tom2",new Date());
    private Gson gson = new Gson();

    /**
     * 添加反序列化参数 类型
     * 采用 第二参数的形式 -- 进行解析
     *
     * {"number":1205,
     *   "userList":[{"id":100,"name":"tom0","birthday":"Jul 5, 2018 4:25:00 PM"},
     *              {"id":101,"name":"tom1","birthday":"Jul 5, 2018 4:25:00 PM"}],
     *    "someone":"wonderful",
     *    "userMap":{"user":{"id":102,"name":"tom2","birthday":"Jul 5, 2018 4:25:00 PM"}}}
     *
     * {number=1205.0, userList=[{id=100.0, name=tom0, birthday=Jul 5, 2018 4:25:00 PM},
     *     {id=101.0, name=tom1, birthday=Jul 5, 2018 4:25:00 PM}],
     *     someone=wonderful,
     *     userMap={user={id=102.0, name=tom2, birthday=Jul 5, 2018 4:25:00 PM}}}
     *
     *
     * {
     *   "number": 1205,
     *   "userList": [
     *     {
     *       "id": 100,
     *       "name": "tom0",
     *       "birthday": "2018-07-05"
     *     },
     *     {
     *       "id": 101,
     *       "name": "tom1",
     *       "birthday": "2018-07-05"
     *     }
     *   ],
     *   "someone": "wonderful",
     *   "userMap": {
     *     "user": {
     *       "id": 102,
     *       "name": "tom2",
     *       "birthday": "2018-07-05"
     *     }
     *   }
     * }
     * [{number=1205.0, userList=[{id=100.0, name=tom0, birthday=2018-07-05},
     *  {id=101.0, name=tom1, birthday=2018-07-05}],
     *  someone=wonderful, userMap={user={id=102.0, name=tom2, birthday=2018-07-05}}}]
     *
     */
    @Test
    public void testMapArgs(){
        List<GsonEntity> list = new ArrayList<>();
        list.add(user0);
        list.add(user1);

        Map<String,Object> muser = new HashMap<>();
        muser.put("user",user2);

        Map<String,Object> map = new HashMap<>();
        map.put("number",1205);
        map.put("someone","wonderful");
        map.put("userList",list);
        map.put("userMap",muser);


        // new Gson() 序列化 和 反序列化
        String jsonArr = gson.toJson(map);
        System.out.println("序列化 :"+jsonArr);
        // 反序列化
        Map<String,Object> usersMap = gson.fromJson(jsonArr,
                new TypeToken<Map<String,Object>>(){}.getType());

        System.out.println("反序列化1:"+usersMap);

        // new GsonBuilder() 序列化 和 反序列化
        gson = new GsonBuilder()
                .setPrettyPrinting()
                .setDateFormat("yyyy-MM-dd")
                .create();
        jsonArr = gson.toJson(map);
        System.out.println("json格式："+jsonArr);
        // 反序列化
        usersMap = gson.fromJson(jsonArr,   new TypeToken<Map<String,Object>>(){}.getType());
        System.out.println("反序列化2:"+Arrays.asList(usersMap));
    }


}

/*
注：TypeToken的构造方法是protected修饰的，
   所以上面才会写成 new TypeToken<Map<String,Object>>(){}.getType()
    而不是new TypeToken<Map<String,Object>>().getType()
 */


